using System;
using System.IO;
using System.Security.Policy;
using NUnit.Framework;

namespace Crawler.Tests
{
	/// <summary>
	/// Summary description for TestDumper.
	/// </summary>
	[TestFixture]
	public class TestDumper
	{
		Dumper dumper;
		private string pathToFile;
		private IScenarioFactory scenarioFactory = ScenarioFactory.scenarioFactory ;

		[SetUp]
		public void Initialise()
		{
			dumper = new Dumper(CentralFactory.centralFactory);
			pathToFile = @"e:\testdata\Sourav\Karan\test\";
			DeleteDirectoryTree(@"e:\testdata") ;
		}

		
		private  void DeleteDirectoryTree(string baseDir)
		{
			string s = pathToFile;
			while(s != baseDir )
			{
				if (Directory.Exists(s))
					Directory.Delete(s , true);
				s = s.Substring(0,s.LastIndexOf('\\')) ; 
			}
			s += "\\";
			foreach(string file in Directory.GetFiles(s) )
			{
				File.Delete(file) ;
			}
		}

		
		private void CreateTestDirectoryStructure()
		{
			Assert.IsTrue(!Directory.Exists(pathToFile));
			dumper.CreateDirectoryStructure(pathToFile);
			Assert.IsTrue(Directory.Exists(pathToFile));
		}

		public void CopyDumpableFile()
		{
			string urlString = @"file:///c:/testdata/3.2 - Planning An Approach.htm";
			string destinationPath = pathToFile + @"testdata\3.2 - Planning An Approach.htm";
			CreateTestDirectoryStructure() ;
			scenarioFactory.Initialize(@"e:\testdata", pathToFile, null) ;
			Assert.IsFalse(File.Exists(@"e:\testdata\3.2 - Planning An Approach.htm")) ;
			scenarioFactory.AddUrlToUrlStackAndTrigger(new Url(urlString), true) ;
			IKey key = new Key(new Uri(urlString).AbsoluteUri.ToLower()); 
			scenarioFactory.AddEntryToMapperAndTrigger(key);
			Assert.IsTrue(File.Exists(pathToFile + @"testdata\3.2 - Planning An Approach.htm")) ;
		}

		public void CopyNonDumpableFile()
		{
			string urlString = @"file:///c:/testdata/my really awesome tree.exe";
			string destinationPath = pathToFile +  @"testdata\my really awesome tree.exe";
			CreateTestDirectoryStructure() ;
			scenarioFactory.Initialize(@"e:\testdata", pathToFile, null) ;
			Assert.IsFalse(File.Exists(@"e:\testdata\my really awesome tree.exe")) ;
			scenarioFactory.AddUrlToUrlStackAndTrigger(new Url(urlString), true) ;
			IKey key = new Key(new Uri(urlString).AbsoluteUri.ToLower()); 
			scenarioFactory.AddEntryToMapperAndTrigger(key);
			Assert.IsTrue(File.Exists(pathToFile + @"testdata\my really awesome tree.exe")) ;
		}


	}
}
